{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "--------\n",
    "* RECORDER:Kakio\n",
    "* week-14\n",
    "* 主要参考文档：\n",
    "> 1. [Python官方文档](https://python.org/doc)\n",
    "> 2. [博客函数](https://www.cnblogs.com/xuzhichao/p/11570285.html)\n",
    "--------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数\n",
    "> 1.函数的定义  \n",
    "> 2.如何使用函数  \n",
    "> 3.函数的返回值  \n",
    "> 4.使用注释改进文档  \n",
    "> 5.传递任意数量的实参  \n",
    "> 6.函数与模块  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 函数的定义\n",
    "\n",
    "------\n",
    "```\n",
    "def;定义函数的关键字\n",
    "fib:函数名\n",
    "(n):定义函数的参数\n",
    "\"\"\" \"\"\":文档描述\n",
    "代码块:\n",
    "    a,b = 0,1\n",
    "    while a < n:\n",
    "        print(a,end=' ')\n",
    "        a,b = b, a+b\n",
    "    print()\n",
    "```\n",
    "------\n",
    "\n",
    "* 定义函数\n",
    "> 1. def定义函数的第一步  \n",
    "> 2. def function_name  \n",
    ">> 1. function_name 遵守变量名称定义的规则  \n",
    "> 3. 函数名称后加上\"():\" ：def function_name():  ===>  f(x) 2x  \n",
    "\n",
    "* 书写函数体\n",
    "> 1. 函数的注释  \n",
    "> 2. 具体操作（函数体）  \n",
    "> 3. 注意：函数也是需要有tab键的间距\n",
    "\n",
    "* 使用函数(函数的调用）\n",
    "> 1. function_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):     # write Fibonacci series up to n \n",
    "    \"\"\"Print a Fibonacci series up to n\"\"\"\n",
    "    a,b = 0,1\n",
    "    while a < n:\n",
    "        print(a,end=' ')\n",
    "        a,b = b, a+b\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 \n"
     ]
    }
   ],
   "source": [
    "fib(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function fib in module __main__:\n",
      "\n",
      "fib(n)\n",
      "    Print a Fibonacci series up to n\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(fib)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 尝试：写一个实现hello world的函数\n",
    "def hello():\n",
    "    \"\"\"Print hello world\"\"\"\n",
    "    print(\"hello world!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 如何使用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 思考函数存在的意义？\n",
    "# 1.可以通过调用函数的方式实现某项功能，使用人不需要考虑函数代码内容\n",
    "# 2.节省时间，提升应用效率\n",
    "# 3.函数一旦被定义，就可以按照一定的规则重复使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"\n",
    "    打印文本中出现的元音字母\n",
    "    请输入您想查询的文本内容\n",
    "    \"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))  # 交集运算\n",
    "    print(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function search4vowels in module __main__:\n",
      "\n",
      "search4vowels(word)\n",
      "    打印文本中出现的元音字母\n",
      "    请输入您想查询的文本内容\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search4vowels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'u', 'a', 'e'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"jdadhque\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "------\n",
    "* 下面的方法太重复了，主要原因是没有对重复功能的代码块有特殊的定义（）\n",
    "------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:dqkjdqi\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'i'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
